<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>

  <body>
    <div id="app"></div>
    <div id="app1"></div>
    <script src="../js/babel.min.js"></script>
    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script type="text/babel">
      class Num extends React.Component {
        // 借助构造器初始化状态
        constructor(props) {
          super(props);
          this.state = { isOne: true };
          // 绑定this
          this.changeState = this.changeState.bind(this);
        }
        changeState() {
          // 这里的this对象是undefined？
          // console.log(this)
          console.log("change");
          // 状态不能直接更改
          // this.state.isOne = !this.state.isOne;
          // 通过setState方法进行修改值，并且通过这种方法进行设置值，只会覆盖原状态下的键值，其余键值不变
          this.setState({ isOne: !this.state.isOne });
          console.log(this);
        }
        render() {
          // console.log(this);
          // 读取状态进行渲染判断
          return (
            // 绑定事件、初始化渲染时不调用函数
            <h2 onClick={this.changeState}>
              this is {this.state.isOne ? "one" : "zero"}
            </h2>
          );
        }
      }
      ReactDOM.render(<Num />, document.getElementById("app1"));
    </script>
  </body>
</html>
